# Enable URL rewriting
RewriteEngine On

# Redirect all www traffic to the non-www domain with HTTPS
RewriteCond %{HTTP_HOST} ^www\.perfectioncoating\.com\.au [NC]
RewriteRule ^(.*)$ https://perfectioncoating.com.au/$1 [R=301,L]

# Redirect all HTTP traffic to HTTPS, excluding sitemap.xml
RewriteCond %{REQUEST_URI} !^/sitemap\.xml
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://perfectioncoating.com.au/$1 [R=301,L]

# Ensure the sitemap.xml is accessible via HTTPS without redirection
RewriteCond %{HTTPS} on
RewriteRule ^sitemap\.xml$ - [L]

# Specify a custom 404 error page
ErrorDocument 404 /404.html

# Set caching rules for images to improve load times
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
</IfModule>

# Set caching rules for CSS to improve load times
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType text/css "access plus 1 month"
</IfModule>

# Set caching rules for JavaScript to improve load times
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>

# Enable HSTS with a max-age of 1 year and include all subdomains
<IfModule mod_headers.c>
    Header set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>